//
//  FSBaseTableViewController.m
//  fanshu
//
//  Created by jklin on 16/3/27.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSBaseTableViewController.h"
#import "FSTableViewGroupItem.h"
#import "FSBaseTableViewCell.h"
#import "FSBasicRowItem.h"

#import "FSArrowRowItem.h"
#import "FSSwitchRowItem.h"

@interface FSBaseTableViewController ()

@end

@implementation FSBaseTableViewController
-(NSMutableArray *)GroupArray{
    if (_GroupArray == nil) {
        NSMutableArray *groupArray = [NSMutableArray array];
        _GroupArray = groupArray;
    }
    return _GroupArray;
}
-(instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.bounces = NO;
    self.view.backgroundColor = [UIColor redColor];
    
    //当cell中有约束时,按约束自动计算高度
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    //取消系统自带分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.GroupArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    FSTableViewGroupItem *groupItem = self.GroupArray[section];
    return groupItem.rowArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //取出当前对象,判断cell的style
    FSTableViewGroupItem *groupItem = self.GroupArray[indexPath.section];
    FSBasicRowItem *rowItem = groupItem.rowArray[indexPath.row];
    
    UITableViewCellStyle style = [rowItem.cellStyle isEqualToString:@"value1"]? UITableViewCellStyleValue1: UITableViewCellStyleValue1;

    FSBaseTableViewCell *cell = [FSBaseTableViewCell BaseCellWithTableView:tableView Style:style];
    [cell setCellWithRowItem:rowItem];
    return cell;
}
//处理点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FSTableViewGroupItem *groupItem = self.GroupArray[indexPath.section];
    FSBasicRowItem *rowItem = groupItem.rowArray[indexPath.row];
    //跳转
    if ([rowItem isKindOfClass:[FSArrowRowItem class]]) {
        FSArrowRowItem *arrowItem = (FSArrowRowItem *)rowItem;
        if (arrowItem.pushVCClass) {
            UIViewController *viewController = [[arrowItem.pushVCClass alloc]init];
            [self.navigationController pushViewController:viewController animated:YES];
        }
        if (arrowItem.eventBlock) {
            arrowItem.eventBlock();
        }
    }
    //开关状态
    if ([rowItem isKindOfClass:[FSSwitchRowItem class]]) {
        FSSwitchRowItem *switchItem = (FSSwitchRowItem *)rowItem;
        //属性添加到沙盒
        [[NSUserDefaults standardUserDefaults]setBool:switchItem.isChoose forKey:switchItem.title];
    }

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    FSTableViewGroupItem *groupItem = self.GroupArray[indexPath.section];
    FSBasicRowItem *rowItem = groupItem.rowArray[indexPath.row];
    if (rowItem.cellHeight) {
        return rowItem.cellHeight;
    }
    if (rowItem.isLoginItem) {
        return 70;
    }
    return [super tableView:tableView heightForRowAtIndexPath:indexPath];;
}
//设置分组的头和尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    FSTableViewGroupItem *groupItem = self.GroupArray[section];
    if (groupItem.headTitle == nil) {
        return 0.01;
    }
    return [super tableView:tableView heightForHeaderInSection:section];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    FSTableViewGroupItem *groupItem = self.GroupArray[section];
    if (groupItem.footTitle == nil) {
        return 10;
    }
    return [super tableView:tableView heightForFooterInSection:section];
}
@end
